package com.le.tester.javaThink.fifth.chapter24_ConcurrentProgramming;

import java.util.Arrays;

/**
 * createTime：2021/8/17 15:15
 * description：简单的Sum
 */
public class Summing2 {
    static long basicSum(long[] ia) {
        long sum = 0;
        int size = ia.length;
        for (long l : ia) {
            sum += l;
        }
        return sum;
    }

    public static final int SZ = 20_000_000;
    public static final long CHECK = (long) SZ * ((long) SZ + 1) / 2;

    public static void main(String[] args) {
        System.out.println(CHECK);
        long[] longs = new long[SZ + 1];
        Arrays.parallelSetAll(longs, i -> i);
        Summing.timeTest("Array Stream Sum", CHECK, () -> Arrays.stream(longs).sum());
        Summing.timeTest("Array Stream Sum Parallel", CHECK, () -> Arrays.stream(longs).parallel().sum());
        Summing.timeTest("Basic Sum", CHECK, () -> basicSum(longs));
        Summing.timeTest("parallelPrefix", CHECK, () -> {
            Arrays.parallelPrefix(longs, Long::sum);
            return longs[longs.length - 1];
        });
    }
}
